package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        0.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        拿到页面的值
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
//        封装页面的值到model
        PageModel pageModel = new PageModel();
        pageModel.setPageNum(pageNum == null || "".equals(pageNum) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize(pageSize == null || "".equals(pageSize) ? 10 : Integer.parseInt(pageSize));
//        调用服务逻辑层
        IProductService iProductService = new ProductServiceImpl();

//        得到数据
        List<ProductModel> list = iProductService.findAll(pageModel);
        int number = iProductService.getCount();
//        返回数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("number", number);
        resp.getWriter().println(jsonObject);

    }
}
